app.controller("z-schedulWeek-c", [ '$scope','$rootScope','$z-http','$sce','z-schedulWeek-s','z-dictionary-s','z-corporation-s','z-department-s','z-punchTime-s','z-employee-s','z-holiday-s','z-edit-menu-s','$filter','$location','downloadUrl',
		function($scope,$rootScope,$zhttp,$sce,Service,DictionaryService,CorporationService,DepartmentService,PunchTimeService,EmployeeService,HolidayService,MenuService,$filter,$location,$downloadUrl) {
		$scope.baseUrl = $location.protocol()+"://"+$location.host()+":"+$location.port()+$downloadUrl;
	
		$scope.viewModel = {};
		$scope.viewModel.perPage = "10";
		$scope.viewModel.rowCount = "1";	
		$scope.viewModel.schedulWeek="";
		$scope.viewModel.currentDate= new Date();
		$scope.viewModel.showSaveBtn=true; //控制保存按钮是否显示
		$scope.viewModel.swName="";
		$scope.viewModel.swType="";
		$scope.viewModel.swYear="";
		$scope.viewModel.swMonth="";
		$scope.viewModel.swYearData = [];
		$scope.viewModel.employeeNo="";
		$scope.viewModel.checkAll=false;
		// 控制是否为批量修改考勤时间
		$scope.viewModel.batchPunchTimeType=false;
		$scope.viewModel.viewIndex=0;

		var swNewYear = new Date().getFullYear();
		for(var year = swNewYear-3;year<=swNewYear+3;year++ ){
			$scope.viewModel.swYearData.push(year);
		}
		
		$scope.editModel = {};
		$scope.editModel.title="";
		$scope.editModel.bodyHeight=null;
		$scope.editModel.schedulWeek={};
		$scope.editModel.schedulWeek.employeePunchTimeList=[];
        $scope.editModel.weeks = new Array("日", "一", "二", "三", "四", "五", "六");
        $scope.editModel.year=null;
        $scope.editModel.month=null;
        $scope.editModel.weeksList=[];
        // 固定班次全选
        $scope.editModel.fixedChecked=false;
        // 无规律班次全选
        $scope.editModel.irregularChecked=false;
        //$scope.editModel.operationType=1;
        
		$scope.viewModel.yearData = [];
		var newYear = new Date().getFullYear();
		for(var year = newYear-10;year<=newYear+10;year++ ){
			$scope.viewModel.yearData.push(year);
		}
        
        //循环设置周
        for(var i=1;i<32;i++){
            var data={};
            data.weeksNumber=i;
            data.weeksName="";
            $scope.editModel.weeksList.push(data);
        }
        
		//日期控件
		$scope.viewModel.dateOptions = {
				formatYear: 'yy',
			    startingDay: 1,
			    showWeeks:false
		};
		//日期控件
		$scope.viewModel.format = "yyyy/MM/dd";
		//日期启动开关
		$scope.viewModel.fromDateOpened= false;
		$scope.viewModel.thruDateOpened= false;
		
		//查询页面名称
		MenuService.getMenuByUrl({url:'schedulWeek'},
		function(data){	
			$scope.viewModel.menuList=data;
		},function(error) {});
		
		//起止日期选择
		$scope.viewModel.fromDateOpen = function($event) {
			/*if($scope.editModel.schedulWeek.swId!=null){
				return;
			}*/
			$event.preventDefault();
			$event.stopPropagation();
			if($scope.viewModel.thruDateOpened){
				$scope.viewModel.thruDateOpened=false;
			}
			$scope.viewModel.fromDateOpened = !$scope.viewModel.fromDateOpened;
		};
		
		$scope.viewModel.thruDateOpen = function($event) {
			/*if($scope.editModel.schedulWeek.swId!=null){
				return;
			}*/
			$event.preventDefault();
			$event.stopPropagation();
			if($scope.viewModel.fromDateOpened){
				$scope.viewModel.fromDateOpened=false;
			}
			$scope.viewModel.thruDateOpened = !$scope.viewModel.thruDateOpened;
		};
		
		//加载考勤状态数据
		$scope.viewModel.bindData = function(){
			$scope.viewModel.employeeList=[];
			if($scope.viewModel.employeeNo!=null){
				var employeeNoData = $scope.viewModel.employeeNo.split("\n");
				for(var i=0;i<employeeNoData.length;i++){
					$scope.viewModel.employeeList.push(employeeNoData[i]);
				}
			}
			$scope.viewModel.isLoading=true;
			Service.getSchedulWeekList({
				pageCount:$scope.viewModel.perPage,
				pageNo:$scope.viewModel.rowCount,
				swName:$scope.viewModel.swName,
				swType:$scope.viewModel.swType,
				swYear:$scope.viewModel.swYear,
				swMonth:$scope.viewModel.swMonth,
				employeeList:$scope.viewModel.employeeList
			},
			function(data){
				$scope.viewModel.dataList = data.schedulWeekBeanList;
				$scope.viewModel.totalData = data.pageTotal;
				$scope.viewModel.addCheckbox();
			    $scope.viewModel.JudgeCheckboxAll();
				$scope.viewModel.isLoading=false;
			},function(error) {
				$scope.viewModel.isLoading=false;		
			});
		};
		$scope.viewModel.bindData();
		
		//判断是否为全选
		$scope.viewModel.JudgeCheckboxAll=function(){
			 var type=false;
			 for(var i=0;i<$scope.viewModel.dataList.length;i++){
				 if($scope.viewModel.dataList[i].checkbox==false){
					 type=false;
					 break;
				 }else{
					 type=true;
				 }
			 }
			 if(type){
				 $scope.viewModel.checkAll=true;
			 }else{
				 $scope.viewModel.checkAll=false;
			 }
		};
		
		//设置默认复选框值
		$scope.viewModel.addCheckbox=function(){
			 for(var i=0;i<$scope.viewModel.dataList.length;i++){
				 $scope.viewModel.dataList[i].checkbox=false;
			 }
		};
		
		//全选事件
		$scope.viewModel.checkboxAll=function(){
			//判断是否为全选
			 if($scope.viewModel.checkAll==false){
				 angular.forEach($scope.viewModel.dataList, function(value, key) {
					 value.checkbox=true;
				 });
			 }else{
				 angular.forEach($scope.viewModel.dataList, function(value, key) {
					 value.checkbox=false;
				 });
			 }
		};
		
		//单选事件
		$scope.viewModel.checkbox=function(schedulWeek){
			 if(schedulWeek.checkbox){
				 schedulWeek.checkbox=false;
			 }else{
				 schedulWeek.checkbox=true;
			 }
			 $scope.viewModel.JudgeCheckboxAll();
		};
		
		// 批量设置考勤时间
		$scope.viewModel.batchPunchTime= function(){
			 var type=true;
			 for(var i=0;i<$scope.viewModel.dataList.length;i++){
				 if($scope.viewModel.dataList[i].checkbox){
					 type=false;
					 break;
				 }
			 }
			 if(type){
				 $rootScope.alertWarning("请选择班次！");
				 return;
			 }
			 $scope.viewModel.batchPunchTimeType=true;
			 $scope.viewPunchTimeModel.load();
		};
		
		// 取消保存
		$scope.viewModel.cancel=function(){
			$scope.viewModel.viewIndex=0;
		};
		
		// 新增
		$scope.viewModel.add = function(swType){
			$scope.editModel.type = true;
			$scope.viewModel.showSaveBtn=true; //控制保存按钮是否显示
			$scope.editModel.schedulWeek={};
			$scope.editModel.schedulWeek.swType=swType;
			$scope.editModel.schedulWeek.swMonday=0;
			$scope.editModel.schedulWeek.swTuesday=0;
			$scope.editModel.schedulWeek.swWednesday=0;
			$scope.editModel.schedulWeek.swThursday=0;
			$scope.editModel.schedulWeek.swFriday=0;
			$scope.editModel.schedulWeek.swSaturday=0;
			$scope.editModel.schedulWeek.swSunday=0;
			$scope.editModel.schedulWeek.isDefault=0;
			$scope.editModel.schedulWeek.punchTimeId=null;
			$scope.editModel.schedulWeek.startFromTime=null;
			$scope.editModel.schedulWeek.employeeIdList=[];
			$scope.editModel.schedulWeek.dateList=[];
			$scope.editModel.schedulWeek.employeePunchTimeList=[];
			//$scope.editModel.operationType=1;
			$scope.editModel.dateStr=null;
			
			$scope.viewEmployeeModel.checkboxDataList=[];
			if(swType==1){
		        // 固定班次全选
		        $scope.editModel.fixedChecked=false;
				$scope.editModel.title="新增周班次";
				$scope.editModel.bodyHeight=520;
				$scope.editModel.schedulWeek.isHoliday=1;
				$scope.editForm.fromDate.$dirty=false;
				$scope.editForm.thruDate.$dirty=false;
			}else{
		        // 无规律班次全选
		        $scope.editModel.irregularChecked=false;
				$scope.editModel.title="新增无规律班次";
				$scope.editModel.bodyHeight=450;
				$scope.editModel.schedulWeek.isHoliday=0;
				//获取当前年份
				$scope.editModel.year = ($scope.viewModel.currentDate).getFullYear();
				var month=($scope.viewModel.currentDate).getMonth()+1+"";
				var strMonth="";
				if(month.length<=1){
					strMonth="0"+month;
				}else{
					strMonth=month;
				}
				$scope.editModel.month=strMonth;
				$scope.viewModel.weekName();
				$scope.editModel.operationSelect();
				//日期绑定值
				$scope.editModel.day01=false;
				$scope.editModel.day02=false;
				$scope.editModel.day03=false;
				$scope.editModel.day04=false;
				$scope.editModel.day05=false;
				$scope.editModel.day06=false;
				$scope.editModel.day07=false;
				$scope.editModel.day08=false;
				$scope.editModel.day09=false;
				$scope.editModel.day10=false;
				$scope.editModel.day11=false;
				$scope.editModel.day12=false;
				$scope.editModel.day13=false;
				$scope.editModel.day14=false;
				$scope.editModel.day15=false;
				$scope.editModel.day16=false;
				$scope.editModel.day17=false;
				$scope.editModel.day18=false;
				$scope.editModel.day19=false;
				$scope.editModel.day20=false;
				$scope.editModel.day21=false;
				$scope.editModel.day22=false;
				$scope.editModel.day23=false;
				$scope.editModel.day24=false;
				$scope.editModel.day25=false;
				$scope.editModel.day26=false;
				$scope.editModel.day27=false;
				$scope.editModel.day28=false;
				$scope.editModel.day29=false;
				$scope.editModel.day30=false;
				$scope.editModel.day31=false;
			}
			//验证不能为空
			$scope.editForm.swName.$dirty=false;
			$scope.viewModel.load();
		};
		
		//修正
		$scope.viewModel.update=function(schedulWeek){
			$scope.viewModel.isLoading=true;
			$scope.editModel.type = false;
			$scope.editModel.schedulWeek=angular.copy(schedulWeek);
			$scope.editModel.schedulWeek.employeePunchTimeList=[];

			//日期绑定值
			$scope.editModel.day01=false;
			$scope.editModel.day02=false;
			$scope.editModel.day03=false;
			$scope.editModel.day04=false;
			$scope.editModel.day05=false;
			$scope.editModel.day06=false;
			$scope.editModel.day07=false;
			$scope.editModel.day08=false;
			$scope.editModel.day09=false;
			$scope.editModel.day10=false;
			$scope.editModel.day11=false;
			$scope.editModel.day12=false;
			$scope.editModel.day13=false;
			$scope.editModel.day14=false;
			$scope.editModel.day15=false;
			$scope.editModel.day16=false;
			$scope.editModel.day17=false;
			$scope.editModel.day18=false;
			$scope.editModel.day19=false;
			$scope.editModel.day20=false;
			$scope.editModel.day21=false;
			$scope.editModel.day22=false;
			$scope.editModel.day23=false;
			$scope.editModel.day24=false;
			$scope.editModel.day25=false;
			$scope.editModel.day26=false;
			$scope.editModel.day27=false;
			$scope.editModel.day28=false;
			$scope.editModel.day29=false;
			$scope.editModel.day30=false;
			$scope.editModel.day31=false;
			
			if(schedulWeek.isDefault==1){
				$scope.viewModel.showSaveBtn=false;
			}else{
				$scope.viewModel.showSaveBtn=true;
			}
			if($scope.editModel.schedulWeek.swType==1){
		        // 固定班次全选
		        $scope.editModel.fixedChecked=false;
				$scope.editModel.title="修改周班次";
				$scope.editModel.bodyHeight=520;
				$scope.viewModel.checkAll
			}else{
		        // 无规律班次全选
		        $scope.editModel.irregularChecked=false;
				$scope.editModel.title="修改无规律班次";
				$scope.editModel.bodyHeight=450;
			}	
			Service.getSchedulWeekBySwId({
				swId:schedulWeek.swId
			},function(data){
				$scope.editModel.schedulWeek.swMonday=data.swMonday;
				$scope.editModel.schedulWeek.swTuesday=data.swTuesday;
				$scope.editModel.schedulWeek.swWednesday=data.swWednesday;
				$scope.editModel.schedulWeek.swThursday=data.swThursday;
				$scope.editModel.schedulWeek.swFriday=data.swFriday;
				$scope.editModel.schedulWeek.swSaturday=data.swSaturday;
				$scope.editModel.schedulWeek.swSunday=data.swSunday;
				$scope.editModel.schedulWeek.employeeIdList=data.employeeIdList;
				$scope.editModel.schedulWeek.dateList=data.dateList;
				$scope.editModel.dateStr=null;
				if($scope.editModel.schedulWeek.swType==2){
					if($scope.editModel.schedulWeek.dateList.length>=0){
						var myDate = new Date($scope.editModel.schedulWeek.dateList[0]);
						$scope.editModel.year=myDate.getFullYear();
						var month=myDate.getMonth()+1+"";
						if(month.length<=1){
							$scope.editModel.month="0"+month;
						}else{
							$scope.editModel.month=month;
						}
						$scope.viewModel.weekName();
						$scope.editModel.operationSelect();
						$scope.editModel.JudgeIrregularCheckedAll();
					}
				}else{
					$scope.editModel.JudgeFixedCheckedAll();
				}
				if(data.fromDate!=null){
					$scope.editModel.schedulWeek.fromDate=new Date(data.fromDate);
				}
				if(data.thruDate!=null){
					$scope.editModel.schedulWeek.thruDate=new Date(data.thruDate);
				}
				if($scope.editModel.schedulWeek.swType){
					$scope.editForm.fromDate.$dirty=false;
					$scope.editForm.thruDate.$dirty=false;
				}
				
				$scope.viewEmployeeModel.checkboxDataList=[];
				if(data.employeeIdList!=null){
					for(var i=0;i<data.employeeIdList.length;i++){
						var employeeIdData={};
						employeeIdData.employeeId=data.employeeIdList[i];
						employeeIdData.checkbox=true;
						$scope.viewEmployeeModel.checkboxDataList.push(employeeIdData);
					}
				}
				
				var employeePunchTimeList=data.employeePunchTimeList;
				if(employeePunchTimeList!=null){
					for(var ai=0;ai<employeePunchTimeList.length;ai++){
						for(var bi=0;bi<$scope.editModel.schedulWeek.employeePunchTimeList.length;bi++){
							if($scope.editModel.schedulWeek.employeePunchTimeList[bi].date.getTime() == employeePunchTimeList[ai].date){
								$scope.editModel.schedulWeek.employeePunchTimeList[bi].checkbox=true;
								$scope.editModel.schedulWeek.employeePunchTimeList[bi].punchTimeId=employeePunchTimeList[ai].punchTimeId;
								$scope.editModel.schedulWeek.employeePunchTimeList[bi].employeeList=employeePunchTimeList[ai].employeeList;
								// 设置选中状态
								var day=$scope.editModel.schedulWeek.employeePunchTimeList[bi].date.getDate();
								if(day=="01"){
									$scope.editModel.day01=true;
								}else if(day=="02"){
									$scope.editModel.day02=true;
								}else if(day=="03"){
									$scope.editModel.day03=true;
								}else if(day=="04"){
									$scope.editModel.day04=true;
								}else if(day=="05"){
									$scope.editModel.day05=true;
								}else if(day=="06"){
									$scope.editModel.day06=true;
								}else if(day=="07"){
									$scope.editModel.day07=true;
								}else if(day=="08"){
									$scope.editModel.day08=true;
								}else if(day=="09"){
									$scope.editModel.day09=true;
								}else if(day=="10"){
									$scope.editModel.day10=true;
								}else if(day=="11"){
									$scope.editModel.day11=true;
								}else if(day=="12"){
									$scope.editModel.day12=true;
								}else if(day=="13"){
									$scope.editModel.day13=true;
								}else if(day=="14"){
									$scope.editModel.day14=true;
								}else if(day=="15"){
									$scope.editModel.day15=true;
								}else if(day=="16"){
									$scope.editModel.day16=true;
								}else if(day=="17"){
									$scope.editModel.day17=true;
								}else if(day=="18"){
									$scope.editModel.day18=true;
								}else if(day=="19"){
									$scope.editModel.day19=true;
								}else if(day=="20"){
									$scope.editModel.day20=true;
								}else if(day=="21"){
									$scope.editModel.day21=true;
								}else if(day=="22"){
									$scope.editModel.day22=true;
								}else if(day=="23"){
									$scope.editModel.day23=true;
								}else if(day=="24"){
									$scope.editModel.day24=true;
								}else if(day=="25"){
									$scope.editModel.day25=true;
								}else if(day=="26"){
									$scope.editModel.day26=true;
								}else if(day=="27"){
									$scope.editModel.day27=true;
								}else if(day=="28"){
									$scope.editModel.day28=true;
								}else if(day=="29"){
									$scope.editModel.day29=true;
								}else if(day=="30"){
									$scope.editModel.day30=true;
								}else if(day=="31"){
									$scope.editModel.day31=true;
								}
								break;
							}
						}
					}
				}
				$scope.viewModel.load();
				$scope.viewModel.isLoading=false;
			},function(error){
				$scope.viewModel.isLoading=false;
				$rootScope.alertError(error.fault.message);
			});
		};
		
		//选中事件
		$scope.viewModel.select=function(){
			$scope.viewModel.weekName();
			//日期绑定值
			$scope.editModel.day01=false;
			$scope.editModel.day02=false;
			$scope.editModel.day03=false;
			$scope.editModel.day04=false;
			$scope.editModel.day05=false;
			$scope.editModel.day06=false;
			$scope.editModel.day07=false;
			$scope.editModel.day08=false;
			$scope.editModel.day09=false;
			$scope.editModel.day10=false;
			$scope.editModel.day11=false;
			$scope.editModel.day12=false;
			$scope.editModel.day13=false;
			$scope.editModel.day14=false;
			$scope.editModel.day15=false;
			$scope.editModel.day16=false;
			$scope.editModel.day17=false;
			$scope.editModel.day18=false;
			$scope.editModel.day19=false;
			$scope.editModel.day20=false;
			$scope.editModel.day21=false;
			$scope.editModel.day22=false;
			$scope.editModel.day23=false;
			$scope.editModel.day24=false;
			$scope.editModel.day25=false;
			$scope.editModel.day26=false;
			$scope.editModel.day27=false;
			$scope.editModel.day28=false;
			$scope.editModel.day29=false;
			$scope.editModel.day30=false;
			$scope.editModel.day31=false;
			$scope.editModel.operationSelect();
		};

		//显示天
		$scope.viewModel.weekName=function(){
			var monthDays = new Date($scope.editModel.year, ($scope.editModel.month), 0).getDate();
			$scope.editModel.monthDays=monthDays;
			//循环设置周
			for(var i=0;i<$scope.editModel.weeksList.length;i++){
				$scope.editModel.mDate = new Date($scope.editModel.year,$scope.editModel.month - 1,$scope.editModel.weeksList[i].weeksNumber);
				$scope.editModel.weeksList[i].weeksName=$scope.editModel.weeks[$scope.editModel.mDate.getDay()];
			}
		};
		
		//加载
		$scope.viewModel.load=function(){
			//$scope.viewModel.show=true;
			$scope.viewModel.viewIndex=$scope.viewModel.viewIndex+1;
		};
		
		//关闭加载
		$scope.viewModel.unLoad=function(){
			//$scope.viewModel.show=false;
			$scope.viewModel.cancel();
			//验证不能为空
			$scope.editForm.swName.$dirty=false;
			$scope.editModel.schedulWeek={};
		};
		
		//验证
		$scope.viewModel.requiredForm= function() {
            if (!$scope.editForm.$valid) {
            	if(!$scope.editForm.swName.$dirty){
            		$scope.editForm.swName.$dirty=true;
            	}
            	// 周班次
            	if($scope.editModel.schedulWeek.swType==1){
	            	if(!$scope.editForm.fromDate.$dirty){
	            		$scope.editForm.fromDate.$dirty=true;
	            	}
	            	if(!$scope.editForm.thruDate.$dirty){
	            		$scope.editForm.thruDate.$dirty=true;
	            	}
            	}else{
            		if($scope.editModel.schedulWeek.swName!=null){
            			return $scope.viewModel.irregular();
            		}
            	}
            	return true;
            }else{
            	// 周班次
            	if($scope.editModel.schedulWeek.swType==1){
                	if($scope.editModel.schedulWeek.fromDate>$scope.editModel.schedulWeek.thruDate){
                		$scope.editForm.thruDate.$invalid=true;
                		return true;
                	}
                	$scope.editForm.fromDate.$invalid=false;
                	$scope.editForm.thruDate.$invalid=false;
                	
                	// 判断是否勾选周班次
                	var swType=true;
        			if($scope.editModel.schedulWeek.swMonday==1){
        				swType=false;
        			}
        			if($scope.editModel.schedulWeek.swTuesday==1){
        				swType=false;	
        			}
        			if($scope.editModel.schedulWeek.swWednesday==1){
        				swType=false;	
        			}
        			if($scope.editModel.schedulWeek.swThursday==1){
        				swType=false;	
        			}
        			if($scope.editModel.schedulWeek.swFriday==1){
        				swType=false;
        			}
        			if($scope.editModel.schedulWeek.swSaturday==1){
        				swType=false;	
        			}
        			if($scope.editModel.schedulWeek.swSunday==1){
        				swType=false;
        			}
        			
        			// true表示未选择周班次
        			if(swType){
        				$rootScope.alertWarning("请选择周班次日期！");
	            		return true;
        			}
                	if($scope.editModel.schedulWeek.employeeIdList.length<=0){
	            		$rootScope.alertWarning("请选择人员！");
	            		return true;
	            	}
	            	if($scope.editModel.schedulWeek.punchTimeId==null){
	               		$rootScope.alertWarning("请选择考勤时间！");
	            		return true;
	            	}
            	}else{
            		return $scope.viewModel.irregular();
            	}
            }
            return false;
        };
        
        // 验证无规律班次时间、员工是否选择
        $scope.viewModel.irregular = function(){
        	// 验收是否选择无规律日期
        	if($scope.editModel.schedulWeek.dateList.length<=0){
        		$rootScope.alertWarning("请选择无规律班次日期！");
        		return true;
        	}
        	// 如果未设置统一的考勤时间，需要验证无规律班次中是否选中考勤时间
			if($scope.editModel.schedulWeek.punchTimeId==null){
    			var type=false;
    			var date;
    			// 验证无规律班次考勤时间是否选择
    			for(var i=0;i<$scope.editModel.schedulWeek.employeePunchTimeList.length;i++){
    				if($scope.editModel.schedulWeek.employeePunchTimeList[i].checkbox==true
    						&& $scope.editModel.schedulWeek.employeePunchTimeList[i].punchTimeId==null){
    					date=$scope.editModel.schedulWeek.employeePunchTimeList[i].date;
						type=true;
						break;
					}
    			}
    			if(type){
        			$rootScope.alertWarning($filter('date')(date,'yyyy-MM-dd')+" 未设置考勤时间，请选择！");
            		return true;
        		}
			}
			
			// 验证无规律班次员工是否选择
			for(var i=0;i<$scope.editModel.schedulWeek.employeePunchTimeList.length;i++){
				if($scope.editModel.schedulWeek.employeePunchTimeList[i].checkbox==true
						&& ($scope.editModel.schedulWeek.employeePunchTimeList[i].employeeList==null
        						|| $scope.editModel.schedulWeek.employeePunchTimeList[i].employeeList.length<=0)){
					date=$scope.editModel.schedulWeek.employeePunchTimeList[i].date;
					type=true;
					break;
				}
			}
			if(type){
    			$rootScope.alertWarning($filter('date')(date,'yyyy-MM-dd')+" 未设置员工，请选择！");
        		return true;
    		}
			return false;
        };
        
		//保存
		//$scope.viewModel.save = function(){
			// 保存
			//$scope.viewModel.save();
			//验证员工是否有其他
			/*Service.getVerifySchedulWeek($scope.editModel.schedulWeek,
					function(){
						$scope.viewModel.saveUpdate();
					},function(error) {
						$rootScope.alertConfirm(error.fault.message,function() {
							$scope.viewModel.saveUpdate();
						});
					});*/
		//};
		
		//保存和修改
		$scope.viewModel.save=function(){
			if($scope.editModel.schedulWeek.swType==2){
				// 设置年/月
				$scope.editModel.schedulWeek.year=$scope.editModel.year;
				$scope.editModel.schedulWeek.month=$scope.editModel.month;
				// 加载无规律日期是否选中
				$scope.editModel.schedulWeek.dateList=[];
				// 处理无规律日期选择状态
				$scope.editModel.isDay();
				// 设置无规律班次开始日期和结束日期
				if($scope.editModel.schedulWeek.dateList.length>0){
					//根据第一选择天数为开始日期，最后天数为结束日期
					$scope.editModel.schedulWeek.fromDate=$scope.editModel.schedulWeek.dateList[0];
					$scope.editModel.schedulWeek.thruDate=$scope.editModel.schedulWeek.dateList[$scope.editModel.schedulWeek.dateList.length-1];
				}
				// 处理使用统一考勤时间
				if($scope.editModel.schedulWeek.punchTimeId!=null){
					for(var i=0;i<$scope.editModel.schedulWeek.employeePunchTimeList.length;i++){
        				if($scope.editModel.schedulWeek.employeePunchTimeList[i].checkbox==true
        						&& $scope.editModel.schedulWeek.employeePunchTimeList[i].punchTimeId==null){
        					$scope.editModel.schedulWeek.employeePunchTimeList[i].punchTimeId=$scope.editModel.schedulWeek.punchTimeId;
						}
        			}
				}
			}
			// 验证必填项
			if($scope.viewModel.requiredForm()){
				return;
			}

			$scope.viewModel.isLoading = true;
			if($scope.editModel.type){
				Service.saveSchedulWeek($scope.editModel.schedulWeek,
						function(data){
							$rootScope.alertInfo("新增成功。");
							$scope.viewModel.bindData();
							$scope.viewModel.unLoad();
							$scope.viewModel.isLoading = false;
						},function(error) {
							//判断是否为数组
							if(angular.isArray(error.fault)){
								var errorData=[];
								errorData.push(error.fault[0].message);
								for(var i=0;i<error.fault[0].errorData.length;i++){
									errorData.push(error.fault[0].errorData[i]);
								}
								$rootScope.alertError(errorData);
							}else{
								$rootScope.alertError(error.fault.message);
							}
							$scope.viewModel.isLoading = false;
						});
			}else{
				Service.updateSchedulWeek($scope.editModel.schedulWeek,
						function(data){
							$scope.viewModel.unLoad();
							$scope.viewModel.bindData();
							$rootScope.alertInfo("修改成功。");
							$scope.viewModel.isLoading = false;
						},function(error) {
							//判断是否为数组
							if(angular.isArray(error.fault)){
								var errorData=[];
								errorData.push(error.fault[0].message);
								for(var i=0;i<error.fault[0].errorData.length;i++){
									errorData.push(error.fault[0].errorData[i]);
								}
								$rootScope.alertError(errorData);
							}else{
								$rootScope.alertError(error.fault.message);
							}
							$scope.viewModel.isLoading = false;
						});
			}
		};
		
		//删除
		$scope.viewModel.del=function(schedulWeek){
			$rootScope.alertConfirm("确定删除班次吗？",function() {
				$scope.viewModel.isLoading=true;
				Service.deleteSchedulWeek({
					swId:schedulWeek.swId
				},function(){
					$scope.viewModel.bindData();
					$scope.viewModel.isLoading=false;
					$rootScope.alertInfo("删除成功。");
				},function(error){
					$scope.viewModel.isLoading=false;
					$rootScope.alertError(error.fault.message);
				});
			});
		};
		
		// 员工明细导出
		$scope.viewModel.exportData= function(schedulWeek) {
			$scope.viewModel.isLoading=true;
        	//导出
			Service.getSchedulWeekURL({
				swId:schedulWeek.swId
			},
			function(data){
				$scope.viewModel.isLoading=false;
				window.location.href = data;
			},function(error) {
				$rootScope.alertError(error.fault.message);
				$scope.viewModel.isLoading=false;
			});
        };
		
		//验证按钮是否可用
		$scope.viewModel.isDisabled=function(schedulWeek){
			if(schedulWeek.isDefault==1){
				return true;
			}
			return false;
		};
		
		//是否可用
		$scope.editModel.disabled=function(schedulWeek){
			if(schedulWeek.swId!=null){
				return true;
			}
			return false;
		};
		
		//是否可用
		$scope.editModel.monthDisabled=function(days){
			//判断如果为修改，复选框不可用
			if($scope.editModel.schedulWeek.swId!=null){
				return true;
			}
			
			//判断小于当前日期下，复选框不可用
			/*var strDate= $scope.editModel.year+"-"+$scope.editModel.month+"-"+days;
			var date=new Date(strDate);
			if(date<$scope.viewModel.currentDate){
				return true;
			}*/
			
			//判断当前最大日期，复选框不可用
			if($scope.editModel.monthDays>=days){
				return false;
			}
			else{
				return true;
			}	
		};
		
		//是否选中
		$scope.editModel.chkChecked=function(value){
			if(value==1){
				return true;
			}
			return false;
		};
		
		// 全选
		$scope.editModel.fixedCheckedAll=function(){
			if($scope.editModel.fixedChecked){
				$scope.editModel.fixedChecked=false;
				$scope.editModel.schedulWeek.swMonday=0;
				$scope.editModel.schedulWeek.swTuesday=0;
				$scope.editModel.schedulWeek.swWednesday=0;
				$scope.editModel.schedulWeek.swThursday=0;
				$scope.editModel.schedulWeek.swFriday=0;
				$scope.editModel.schedulWeek.swSaturday=0;
				$scope.editModel.schedulWeek.swSunday=0;
			}else{
				$scope.editModel.fixedChecked=true;
				$scope.editModel.schedulWeek.swMonday=1;
				$scope.editModel.schedulWeek.swTuesday=1;
				$scope.editModel.schedulWeek.swWednesday=1;
				$scope.editModel.schedulWeek.swThursday=1;
				$scope.editModel.schedulWeek.swFriday=1;
				$scope.editModel.schedulWeek.swSaturday=1;
				$scope.editModel.schedulWeek.swSunday=1;
			}
		};
		
		// 监控是否固定班次全选
		$scope.editModel.JudgeFixedCheckedAll=function(){
			if($scope.editModel.JudgeFixedCheckedState()){
				$scope.editModel.fixedChecked=true;
			}else{
				$scope.editModel.fixedChecked=false;
			}
		};
		
		// 监控固定复选框状态
		$scope.editModel.JudgeFixedCheckedState=function(){
			if($scope.editModel.schedulWeek.swMonday==0){
				return false;
			}
			if($scope.editModel.schedulWeek.swTuesday==0){
				return false;
			}
			if($scope.editModel.schedulWeek.swWednesday==0){
				return false;
			}
			if($scope.editModel.schedulWeek.swThursday==0){
				return false;
			}
			if($scope.editModel.schedulWeek.swFriday==0){
				return false;
			}
			if($scope.editModel.schedulWeek.swSaturday==0){
				return false;
			}
			if($scope.editModel.schedulWeek.swSunday==0){
				return false;
			}
			return true;
		}
		
		//点击
		$scope.editModel.isWeekCheckbox=function(number){
			if(number==1){
				if($scope.editModel.schedulWeek.swMonday==0){
					$scope.editModel.schedulWeek.swMonday=1;
				}else{
					$scope.editModel.schedulWeek.swMonday=0;
				}
			}else if(number==2){
				if($scope.editModel.schedulWeek.swTuesday==0){
					$scope.editModel.schedulWeek.swTuesday=1;
				}else{
					$scope.editModel.schedulWeek.swTuesday=0;
				}
			}else if(number==3){
				if($scope.editModel.schedulWeek.swWednesday==0){
					$scope.editModel.schedulWeek.swWednesday=1;
				}else{
					$scope.editModel.schedulWeek.swWednesday=0;
				}
			}else if(number==4){
				if($scope.editModel.schedulWeek.swThursday==0){
					$scope.editModel.schedulWeek.swThursday=1;
				}else{
					$scope.editModel.schedulWeek.swThursday=0;
				}
			}else if(number==5){
				if($scope.editModel.schedulWeek.swFriday==0){
					$scope.editModel.schedulWeek.swFriday=1;
				}else{
					$scope.editModel.schedulWeek.swFriday=0;
				}
			}else if(number==6){
				if($scope.editModel.schedulWeek.swSaturday==0){
					$scope.editModel.schedulWeek.swSaturday=1;
				}else{
					$scope.editModel.schedulWeek.swSaturday=0;
				}
			}else if(number==7){
				if($scope.editModel.schedulWeek.swSunday==0){
					$scope.editModel.schedulWeek.swSunday=1;
				}else{
					$scope.editModel.schedulWeek.swSunday=0;
				}
			}
			$scope.editModel.JudgeFixedCheckedAll();
		};
		
		// 无规律全选
		$scope.editModel.irregularCheckedAll=function(){
			if($scope.editModel.irregularChecked){
				$scope.editModel.irregularChecked=false;
				//日期绑定值
				$scope.editModel.day01=false;
				$scope.editModel.day02=false;
				$scope.editModel.day03=false;
				$scope.editModel.day04=false;
				$scope.editModel.day05=false;
				$scope.editModel.day06=false;
				$scope.editModel.day07=false;
				$scope.editModel.day08=false;
				$scope.editModel.day09=false;
				$scope.editModel.day10=false;
				$scope.editModel.day11=false;
				$scope.editModel.day12=false;
				$scope.editModel.day13=false;
				$scope.editModel.day14=false;
				$scope.editModel.day15=false;
				$scope.editModel.day16=false;
				$scope.editModel.day17=false;
				$scope.editModel.day18=false;
				$scope.editModel.day19=false;
				$scope.editModel.day20=false;
				$scope.editModel.day21=false;
				$scope.editModel.day22=false;
				$scope.editModel.day23=false;
				$scope.editModel.day24=false;
				$scope.editModel.day25=false;
				$scope.editModel.day26=false;
				$scope.editModel.day27=false;
				$scope.editModel.day28=false;
				$scope.editModel.day29=false;
				$scope.editModel.day30=false;
				$scope.editModel.day31=false;
			}else{
				$scope.editModel.irregularChecked=true;
				//日期绑定值
				$scope.editModel.day01=true;
				$scope.editModel.day02=true;
				$scope.editModel.day03=true;
				$scope.editModel.day04=true;
				$scope.editModel.day05=true;
				$scope.editModel.day06=true;
				$scope.editModel.day07=true;
				$scope.editModel.day08=true;
				$scope.editModel.day09=true;
				$scope.editModel.day10=true;
				$scope.editModel.day11=true;
				$scope.editModel.day12=true;
				$scope.editModel.day13=true;
				$scope.editModel.day14=true;
				$scope.editModel.day15=true;
				$scope.editModel.day16=true;
				$scope.editModel.day17=true;
				$scope.editModel.day18=true;
				$scope.editModel.day19=true;
				$scope.editModel.day20=true;
				$scope.editModel.day21=true;
				$scope.editModel.day22=true;
				$scope.editModel.day23=true;
				$scope.editModel.day24=true;
				$scope.editModel.day25=true;
				$scope.editModel.day26=true;
				$scope.editModel.day27=true;
				$scope.editModel.day28=true;
				$scope.editModel.day29=true;
				$scope.editModel.day30=true;
				$scope.editModel.day31=true;
			}
		};
		
		// 无规律单选
		$scope.editModel.isIrregularWeekCheckbox=function(day){
			if(day=="01"){
				if($scope.editModel.day01){
					$scope.editModel.day01=false;
				}else{
					$scope.editModel.day01=true;
				}
			}else if(day=="02"){
				if($scope.editModel.day02){
					$scope.editModel.day02=false;
				}else{
					$scope.editModel.day02=true;
				}
			}
			else if(day=="03"){
				if($scope.editModel.day03){
					$scope.editModel.day03=false;
				}else{
					$scope.editModel.day03=true;
				}
			}else if(day=="04"){
				if($scope.editModel.day04){
					$scope.editModel.day04=false;
				}else{
					$scope.editModel.day04=true;
				}
			}else if(day=="05"){
				if($scope.editModel.day05){
					$scope.editModel.day05=false;
				}else{
					$scope.editModel.day05=true;
				}
			}else if(day=="06"){
				if($scope.editModel.day06){
					$scope.editModel.day06=false;
				}else{
					$scope.editModel.day06=true;
				}
			}else if(day=="07"){
				if($scope.editModel.day07){
					$scope.editModel.day07=false;
				}else{
					$scope.editModel.day07=true;
				}
			}else if(day=="08"){
				if($scope.editModel.day08){
					$scope.editModel.day08=false;
				}else{
					$scope.editModel.day08=true;
				}
			}else if(day=="09"){
				if($scope.editModel.day09){
					$scope.editModel.day09=false;
				}else{
					$scope.editModel.day09=true;
				}
			}else if(day=="10"){
				if($scope.editModel.day10){
					$scope.editModel.day10=false;
				}else{
					$scope.editModel.day10=true;
				}
			}else if(day=="11"){
				if($scope.editModel.day11){
					$scope.editModel.day11=false;
				}else{
					$scope.editModel.day11=true;
				}
			}else if(day=="12"){
				if($scope.editModel.day12){
					$scope.editModel.day12=false;
				}else{
					$scope.editModel.day12=true;
				}
			}else if(day=="13"){
				if($scope.editModel.day13){
					$scope.editModel.day13=false;
				}else{
					$scope.editModel.day13=true;
				}
			}else if(day=="14"){
				if($scope.editModel.day14){
					$scope.editModel.day14=false;
				}else{
					$scope.editModel.day14=true;
				}
			}else if(day=="15"){
				if($scope.editModel.day15){
					$scope.editModel.day15=false;
				}else{
					$scope.editModel.day15=true;
				}
			}else if(day=="16"){
				if($scope.editModel.day16){
					$scope.editModel.day16=false;
				}else{
					$scope.editModel.day16=true;
				}
			}else if(day=="17"){
				if($scope.editModel.day17){
					$scope.editModel.day17=false;
				}else{
					$scope.editModel.day17=true;
				}
			}else if(day=="18"){
				if($scope.editModel.day18){
					$scope.editModel.day18=false;
				}else{
					$scope.editModel.day18=true;
				}
			}else if(day=="19"){
				if($scope.editModel.day19){
					$scope.editModel.day19=false;
				}else{
					$scope.editModel.day19=true;
				}
			}else if(day=="20"){
				if($scope.editModel.day20){
					$scope.editModel.day20=false;
				}else{
					$scope.editModel.day20=true;
				}
			}else if(day=="21"){
				if($scope.editModel.day21){
					$scope.editModel.day21=false;
				}else{
					$scope.editModel.day21=true;
				}
			}else if(day=="22"){
				if($scope.editModel.day22){
					$scope.editModel.day22=false;
				}else{
					$scope.editModel.day22=true;
				}
			}else if(day=="23"){
				if($scope.editModel.day23){
					$scope.editModel.day23=false;
				}else{
					$scope.editModel.day23=true;
				}
			}else if(day=="24"){
				if($scope.editModel.day24){
					$scope.editModel.day24=false;
				}else{
					$scope.editModel.day24=true;
				}
			}else if(day=="25"){
				if($scope.editModel.day25){
					$scope.editModel.day25=false;
				}else{
					$scope.editModel.day25=true;
				}
			}else if(day=="26"){
				if($scope.editModel.day26){
					$scope.editModel.day26=false;
				}else{
					$scope.editModel.day26=true;
				}
			}else if(day=="27"){
				if($scope.editModel.day27){
					$scope.editModel.day27=false;
				}else{
					$scope.editModel.day27=true;
				}
			}else if(day=="28"){
				if($scope.editModel.day28){
					$scope.editModel.day28=false;
				}else{
					$scope.editModel.day28=true;
				}
			}else if(day=="29"){
				if($scope.editModel.day29){
					$scope.editModel.day29=false;
				}else{
					$scope.editModel.day29=true;
				}
			}else if(day=="30"){
				if($scope.editModel.day30){
					$scope.editModel.day30=false;
				}else{
					$scope.editModel.day30=true;
				}
			}else if(day=="31"){
				if($scope.editModel.day31){
					$scope.editModel.day31=false;
				}else{
					$scope.editModel.day31=true;
				}
			}
			$scope.editModel.JudgeIrregularCheckedAll();
		};

		// 监控是否无规律班次全选
		$scope.editModel.JudgeIrregularCheckedAll=function(){
			if($scope.editModel.JudgeIrregularCheckedState()){
				$scope.editModel.irregularChecked=true;
			}else{
				$scope.editModel.irregularChecked=false;
			}
		};
		
		// 监控无规律复选框状态
		$scope.editModel.JudgeIrregularCheckedState=function(){
			if($scope.editModel.day01==false){
				return false;
			}
			if($scope.editModel.day02==false){
				return false;
			}
			if($scope.editModel.day03==false){
				return false;
			}
			if($scope.editModel.day04==false){
				return false;
			}
			if($scope.editModel.day05==false){
				return false;
			}
			if($scope.editModel.day06==false){
				return false;
			}
			if($scope.editModel.day07==false){
				return false;
			}
			if($scope.editModel.day08==false){
				return false;
			}
			if($scope.editModel.day09==false){
				return false;
			}
			if($scope.editModel.day10==false){
				return false;
			}
			if($scope.editModel.day11==false){
				return false;
			}
			if($scope.editModel.day12==false){
				return false;
			}
			if($scope.editModel.day13==false){
				return false;
			}
			if($scope.editModel.day14==false){
				return false;
			}
			if($scope.editModel.day15==false){
				return false;
			}
			if($scope.editModel.day16==false){
				return false;
			}
			if($scope.editModel.day17==false){
				return false;
			}
			if($scope.editModel.day18==false){
				return false;
			}
			if($scope.editModel.day19==false){
				return false;
			}
			if($scope.editModel.day20==false){
				return false;
			}
			if($scope.editModel.day21==false){
				return false;
			}
			if($scope.editModel.day22==false){
				return false;
			}
			if($scope.editModel.day23==false){
				return false;
			}
			if($scope.editModel.day24==false){
				return false;
			}
			if($scope.editModel.day25==false){
				return false;
			}
			if($scope.editModel.day26==false){
				return false;
			}
			if($scope.editModel.day27==false){
				return false;
			}
			if($scope.editModel.day28==false){
				return false;
			}
			if($scope.editModel.day29==false){
				return false;
			}
			if($scope.editModel.day30==false){
				return false;
			}
			if($scope.editModel.day31==false){
				return false;
			}
			return true;
		};
		
		//判断每月天数是否选中
		$scope.editModel.isCheckbox=function(dayNum){
			if($scope.editModel.schedulWeek.dateList!=null){
				for(var i=0;i<$scope.editModel.schedulWeek.dateList.length;i++){
					var myDate = new Date($scope.editModel.schedulWeek.dateList[i]);
					var date=myDate.getDate();
					var strDate=null;
					if(date.length<=1){
						strDate="0"+date;
					}else{
						strDate=date;
					}
					if(strDate==dayNum){
						return true;
					}
				}
				return false;
			}
		};
		
		//月份天数选中时执行
		$scope.editModel.isDay=function(){
			 if($scope.editModel.day01){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/01");
			 }
			 if($scope.editModel.day02){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/02");
			 }
			 if($scope.editModel.day03){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/03");
			 }
			 if($scope.editModel.day04){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/04");
			 }
			 if($scope.editModel.day05){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/05");
			 }
			 if($scope.editModel.day06){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/06");
			 }
			 if($scope.editModel.day07){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/07");
			 }
			 if($scope.editModel.day08){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/08");
			 }
			 if($scope.editModel.day09){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/09");
			 }
			 if($scope.editModel.day10){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/10");
			 }
			 if($scope.editModel.day11){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/11");
			 }
			 if($scope.editModel.day12){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/12");
			 }
			 if($scope.editModel.day13){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/13");
			 }
			 if($scope.editModel.day14){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/14");
			 }
			 if($scope.editModel.day15){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/15");
			 }
			 if($scope.editModel.day16){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/16");
			 }
			 if($scope.editModel.day17){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/17");
			 }
			 if($scope.editModel.day18){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/18");
			 }
			 if($scope.editModel.day19){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/19");
			 }
			 if($scope.editModel.day20){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/20");
			 }
			 if($scope.editModel.day21){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/21");
			 }
			 if($scope.editModel.day22){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/22");
			 }
			 if($scope.editModel.day23){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/23");
			 }
			 if($scope.editModel.day24){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/24");
			 }
			 if($scope.editModel.day25){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/25");
			 }
			 if($scope.editModel.day26){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/26");
			 }
			 if($scope.editModel.day27){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/27");
			 }
			 if($scope.editModel.day28){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/28");
			 }
			 if($scope.editModel.day29){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/29");
			 }
			 if($scope.editModel.day30){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/30");
			 }
			 if($scope.editModel.day31){
				 $scope.editModel.addDateList($scope.editModel.year+"/"+$scope.editModel.month+"/31");
			 }
			 
			 // 循环无规律日期集合，用于设置是否选中
			 for(var i=0;i<$scope.editModel.schedulWeek.employeePunchTimeList.length;i++){
				 $scope.editModel.schedulWeek.employeePunchTimeList[i].checkbox=false;
				 // 循环选中无规律日期集合
				 for(var j=0;j<$scope.editModel.schedulWeek.dateList.length;j++){
					 if($scope.editModel.schedulWeek.dateList[j].getTime() ==  $scope.editModel.schedulWeek.employeePunchTimeList[i].date.getTime()){
						 $scope.editModel.schedulWeek.employeePunchTimeList[i].checkbox=true;
						 break;
					 }
				 }
			 } 
		};
		
		//添加日期
		$scope.editModel.addDateList=function(strDate){
			var date = new Date(Date.parse(strDate));   
			$scope.editModel.schedulWeek.dateList.push(date);
		};
		
		//选择事件
		$scope.editModel.operationSelect=function(){
		    if($scope.editModel.schedulWeek.swType==2){	
				$scope.editModel.schedulWeek.startFromTime=null;
				$scope.editModel.schedulWeek.employeePunchTimeList=[];
				if($scope.editModel.year!=null && $scope.editModel.month!=null){
					for(var ei=0;ei<$scope.editModel.monthDays;ei++){
						var number=ei+1;
						var dateStr=$scope.editModel.year+"/"+$scope.editModel.month+"/";
						if(number<=9){
							dateStr=dateStr+"0"+number;
						}else{
							dateStr=dateStr+number;
						}
						var data={};
						data.date=new Date(dateStr);
						data.punchTimeId=null;
						data.checkbox=false;
						data.employeeList=[];
						$scope.editModel.schedulWeek.employeePunchTimeList.push(data);
					}
				}
			}else{
				$scope.editModel.schedulWeek.employeePunchTimeList=[];
			}
		};
		
		// 开启选择考勤时间
		$scope.editModel.openPunchTime=function(weeksNumber){
			$scope.editModel.calculationWeeksNumber(weeksNumber);
			$scope.viewPunchTimeModel.punchTimeType=false;
			$scope.viewPunchTimeModel.load();
		};
		
		// 清空选择考勤时间
		$scope.editModel.delPunchTime=function(weeksNumber){
			$rootScope.alertConfirm("确定删除考勤时间吗？",function() {
				$scope.editModel.calculationWeeksNumber(weeksNumber);
				var date=new Date($scope.editModel.dateStr);
				for(var ei=0;ei<$scope.editModel.schedulWeek.employeePunchTimeList.length;ei++){
					if($scope.editModel.schedulWeek.employeePunchTimeList[ei].date.getTime()== date.getTime()){
						$scope.editModel.schedulWeek.employeePunchTimeList[ei].punchTimeId=null;
						break;
					}
				}
			});
		};
		
		// 开启选择人员
		$scope.editModel.openEmployee=function(weeksNumber){
			$scope.editModel.calculationWeeksNumber(weeksNumber);
			$scope.viewEmployeeModel.checkboxDataList=[];
			var date=new Date($scope.editModel.dateStr);
			for(var ei=0;ei<$scope.editModel.schedulWeek.employeePunchTimeList.length;ei++){
				if($scope.editModel.schedulWeek.employeePunchTimeList[ei].date.getTime()== date.getTime()){
					if($scope.editModel.schedulWeek.employeePunchTimeList[ei].employeeList!=null){
						for(var i=0;i<$scope.editModel.schedulWeek.employeePunchTimeList[ei].employeeList.length;i++){
							$scope.viewEmployeeModel.checkboxDataList.push($scope.editModel.schedulWeek.employeePunchTimeList[ei].employeeList[i]);
						}
					}
					break;
				}
			}
			$scope.viewEmployeeModel.employeeType=false;
			$scope.viewEmployeeModel.load();
		};
		
		// 删除选择人员
		$scope.editModel.delEmployee=function(weeksNumber){
			$rootScope.alertConfirm("确定删除员工吗？",function() {
				$scope.editModel.calculationWeeksNumber(weeksNumber);
				var date=new Date($scope.editModel.dateStr);
				for(var ei=0;ei<$scope.editModel.schedulWeek.employeePunchTimeList.length;ei++){
					if($scope.editModel.schedulWeek.employeePunchTimeList[ei].date.getTime()== date.getTime()){
						$scope.editModel.schedulWeek.employeePunchTimeList[ei].employeeList=[];
						break;
					}
				}
			});
		};
		
		// 开始选择串休
		$scope.editModel.openRest=function(weeksNumber){
			$scope.editModel.calculationWeeksNumber(weeksNumber);
			var date=new Date($scope.editModel.dateStr);
			for(var ei=0;ei<$scope.editModel.schedulWeek.employeePunchTimeList.length;ei++){
				if($scope.editModel.schedulWeek.employeePunchTimeList[ei].date.getTime()== date.getTime()){
					$scope.viewSwRestModel.swId=$scope.editModel.schedulWeek.swId;
					$scope.viewSwRestModel.date=$scope.editModel.schedulWeek.employeePunchTimeList[ei].date;
					$scope.viewSwRestModel.employeeId=$scope.editModel.schedulWeek.employeePunchTimeList[ei].employeeId;
					$scope.viewSwRestModel.employeeIdRest=$scope.editModel.schedulWeek.employeePunchTimeList[ei].employeeIdRest;
					break;
				}
			}
			$scope.viewSwRestModel.load();
		};
		
		// 删除选择串休
		$scope.editModel.delRest=function(weeksNumber){
			$rootScope.alertConfirm("确定删除串休员工吗？",function() {
				$scope.editModel.calculationWeeksNumber(weeksNumber);
				var date=new Date($scope.editModel.dateStr);
				for(var ei=0;ei<$scope.editModel.schedulWeek.employeePunchTimeList.length;ei++){
					if($scope.editModel.schedulWeek.employeePunchTimeList[ei].date.getTime()== date.getTime()){
						$scope.editModel.schedulWeek.employeePunchTimeList[ei].employeeId=null;
						$scope.editModel.schedulWeek.employeePunchTimeList[ei].employeeIdRest=null;
						$scope.editModel.schedulWeek.employeePunchTimeList[ei].dateRest=null;
						break;
					}
				}
			});
		};
		
		// 计算无规律选择日期
		$scope.editModel.calculationWeeksNumber=function(weeksNumber){
			$scope.editModel.dateStr=$scope.editModel.year+"/"+$scope.editModel.month+"/";
			if(weeksNumber<=9){
				$scope.editModel.dateStr=$scope.editModel.dateStr+"0"+weeksNumber;
			}else{
				$scope.editModel.dateStr=$scope.editModel.dateStr+weeksNumber;
			}
		};
		
		//控制按钮是否可用
		$scope.editModel.operationDisabled=function(weeksNumber){
			//判断如果为修改，复选框不可用
		    /*if($scope.editModel.schedulWeek.swId!=null){
				return false;
			}*/
			//判断当前最大日期，复选框不可用
			//if($scope.editModel.monthDays>=weeksNumber){
				 //判断是否选中
				 if($scope.editModel.day01 && weeksNumber==1){
					 return false;
				 }else if($scope.editModel.day02 && weeksNumber==2){
					 return false;
				 }else if($scope.editModel.day03 && weeksNumber==3){
					 return false;
				 }else if($scope.editModel.day04 && weeksNumber==4){
					 return false;
				 }else if($scope.editModel.day05 && weeksNumber==5){
					 return false;
				 }else if($scope.editModel.day06 && weeksNumber==6){
					 return false;
				 }else if($scope.editModel.day07 && weeksNumber==7){
					 return false;
				 }else if($scope.editModel.day08 && weeksNumber==8){
					 return false;
				 }else if($scope.editModel.day09 && weeksNumber==9){
					 return false;
				 }else if($scope.editModel.day10 && weeksNumber==10){
					 return false;
				 }else if($scope.editModel.day11 && weeksNumber==11){
					 return false;
				 }else if($scope.editModel.day12 && weeksNumber==12){
					 return false;
				 }else if($scope.editModel.day13 && weeksNumber==13){
					 return false;
				 }else if($scope.editModel.day14 && weeksNumber==14){
					 return false;
				 }else if($scope.editModel.day15 && weeksNumber==15){
					 return false;
				 }else if($scope.editModel.day16 && weeksNumber==16){
					 return false;
				 }else if($scope.editModel.day17 && weeksNumber==17){
					 return false;
				 }else if($scope.editModel.day18 && weeksNumber==18){
					 return false;
				 }else if($scope.editModel.day19 && weeksNumber==19){
					 return false;
				 }else if($scope.editModel.day20 && weeksNumber==20){
					 return false;
				 }else if($scope.editModel.day21 && weeksNumber==21){
					 return false;
				 }else if($scope.editModel.day22 && weeksNumber==22){
					 return false;
				 }else if($scope.editModel.day23 && weeksNumber==23){
					 return false;
				 }else if($scope.editModel.day24 && weeksNumber==24){
					 return false;
				 }else if($scope.editModel.day25 && weeksNumber==25){
					 return false;
				 }else if($scope.editModel.day26 && weeksNumber==26){
					 return false;
				 }else if($scope.editModel.day27 && weeksNumber==27){
					 return false;
				 }else if($scope.editModel.day28 && weeksNumber==28){
					 return false;
				 }else if($scope.editModel.day29 && weeksNumber==29){
					 return false;
				 }else if($scope.editModel.day30 && weeksNumber==30){
					 return false;
				 }else if($scope.editModel.day31 && weeksNumber==31){
					 return false;
				 }else{
					 return true;
				 }
			/*}
			else{
				return true;
			}	*/
		};
		
		// 控制按钮选择颜色
		$scope.editModel.operationClass=function(weeksNumber,typeName){
			var dateStr=$scope.editModel.year+"/"+$scope.editModel.month+"/";
			if(weeksNumber<=9){
				dateStr=dateStr+"0"+weeksNumber;
			}else{
				dateStr=dateStr+weeksNumber;
			}
			var className="btn btn-success btn-sm bt-margin-bottom";
			if(dateStr!=null){
				var date2=new Date(dateStr);
				for(var ei=0;ei<$scope.editModel.schedulWeek.employeePunchTimeList.length;ei++){
					if($scope.editModel.schedulWeek.employeePunchTimeList[ei].date.getTime()== date2.getTime()){
						// 时间
						if(typeName=="punchTime"){
							if($scope.editModel.schedulWeek.employeePunchTimeList[ei].punchTimeId!=null){
								className="btn btn-primary btn-sm bt-margin-bottom";
								break; 
							}
						}
						// 员工
						else if(typeName=="employee"){
							if($scope.editModel.schedulWeek.employeePunchTimeList[ei].employeeList!=null
									&& $scope.editModel.schedulWeek.employeePunchTimeList[ei].employeeList.length>0){
								className="btn btn-primary btn-sm bt-margin-bottom";
								break; 
							}
						}
						// 串休
						else if(typeName=="rest"){
							if($scope.editModel.schedulWeek.employeePunchTimeList[ei].employeeId!=null
									&& $scope.editModel.schedulWeek.employeePunchTimeList[ei].employeeIdRest!=null
									&& $scope.editModel.schedulWeek.employeePunchTimeList[ei].date!=null
									&& $scope.editModel.schedulWeek.employeePunchTimeList[ei].dateRest!=null){
								className="btn btn-primary btn-sm bt-margin-bottom";
								break; 
							}
						}
					}
				}
			}
			return className;
		};
		
		// 控制清空按钮是否显示
		$scope.editModel.operationShow=function(weeksNumber,typeName){
			var dateStr=$scope.editModel.year+"/"+$scope.editModel.month+"/";
			if(weeksNumber<=9){
				dateStr=dateStr+"0"+weeksNumber;
			}else{
				dateStr=dateStr+weeksNumber;
			}
			var type=true;
			if(dateStr!=null){
				var date2=new Date(dateStr);
				for(var ei=0;ei<$scope.editModel.schedulWeek.employeePunchTimeList.length;ei++){
					if($scope.editModel.schedulWeek.employeePunchTimeList[ei].date.getTime()== date2.getTime()){
						// 时间
						if(typeName=="punchTime"){
							if($scope.editModel.schedulWeek.employeePunchTimeList[ei].punchTimeId!=null){
								type=false;
								break; 
							}
						}
						// 员工
						else if(typeName=="employee"){
							if($scope.editModel.schedulWeek.employeePunchTimeList[ei].employeeList!=null
									&& $scope.editModel.schedulWeek.employeePunchTimeList[ei].employeeList.length>0){
								type=false;
								break; 
							}
						}
						// 串休
						else if(typeName=="rest"){
							if($scope.editModel.schedulWeek.employeePunchTimeList[ei].employeeId!=null
									&& $scope.editModel.schedulWeek.employeePunchTimeList[ei].employeeIdRest!=null
									&& $scope.editModel.schedulWeek.employeePunchTimeList[ei].dateRest!=null){
								type=false;
								break; 
							}
						}
					}
				}
			}
			return type;
		};
		
		/************人员*************/
		//选择员工
		$scope.viewEmployeeModel={};
		$scope.viewEmployeeModel.employeeNo="";
	    $scope.viewEmployeeModel.departmentName="";
	    $scope.viewEmployeeModel.roleName="";
		$scope.viewEmployeeModel.perPage="1";
		$scope.viewEmployeeModel.rowCount="10";
		$scope.viewEmployeeModel.title="选择人员";
		$scope.viewEmployeeModel.employeeCount=0;
		// 定义当前时间
		$scope.viewEmployeeModel.effectiveDate = new Date();
		//默认员工集合
		$scope.viewEmployeeModel.checkboxDataList=[];
		// 控制开启考勤时间窗口状态
		// true 选择（按钮触发） false 人员（按钮触发）
		$scope.viewEmployeeModel.employeeType=true;
		
		// 选择
		$scope.viewEmployeeModel.addEmployee=function(){
			$scope.viewEmployeeModel.employeeType=true;
			$scope.viewEmployeeModel.load();
		};
		
		//加载
		$scope.viewEmployeeModel.load=function(){
			$scope.viewEmployeeModel.employeeIsSeparated=null;
			$scope.viewEmployeeModel.regionType=null;
			$scope.viewEmployeeModel.employeeNo=null;
			$scope.viewEmployeeModel.roleName=null;
			$scope.viewEmployeeModel.departmentList=[];
			$scope.viewEmployeeModel.corporationList=[];
			$scope.viewEmployeeModel.employeeList=[];
			$scope.viewEmployeeModel.employeeLevelList=[];
			$scope.viewEmployeeModel.level=null;
			$scope.viewEmployeeModel.show=true;
			$scope.viewEmployeeModel.bindData();
			$(".multiselect-selected-text").text("无");
			//下拉多选框长度
			$scope.multiSelectMaxHeight=300;
			//加载信息
			$scope.viewEmployeeModel.bindSelectRegionTypeData = function(){
				//查询公司
				CorporationService.getCorporationList({
					fromDate:$scope.viewEmployeeModel.effectiveDate,
				},
				function(data){
					var site = [];  
			        $.each(data, function(index, values) {  
			            site.push({  
			                label : "("+values.corporationCode+")"+values.corporationName,  
			                value : values.corporationCode  
			            });  
			        });  
			        $('#sweCorporation').multiselect('dataprovider',site);
				},function(error) {
					
				});
			};
			$scope.viewEmployeeModel.bindSelectRegionTypeData();
			
		
			/*******使用为下拉带勾选控件*********/
			//员工级别
			$('#swelevel').multiselect({
				includeSelectAllOption: true,
				enableFiltering: true,
				delimiterText: '; ',
	            maxHeight: $scope.multiSelectMaxHeight
	        });
			//公司
			$('#sweCorporation').multiselect({
				includeSelectAllOption: true,
				enableFiltering: true,
				delimiterText: '; ',
	            maxHeight: $scope.multiSelectMaxHeight
	        });
			//公司选中事件
			$("#sweCorporation").change(function(){  
				var value=$(this).val();
			    var data={};
			    data.corporationCodeList=value;
			    data.fromDate = $scope.viewEmployeeModel.effectiveDate;
			    data.pageName="schedulWeek";
			    if(value==null){
			    	var site = [];  
			    	$('#sweDepartmentName').multiselect('dataprovider',site);
			    	$scope.viewEmployeeModel.departmentList=[];
			    }else{
			    	//查询部门
				    DepartmentService.getDepartmentListByCorporationCodeList(data,
					function(data){
				    	var site = [];  
				        $.each(data, function(index, values) {  
				            site.push({  
				                label : "("+values.departmentCode+")"+values.departmentName,  
				                value : values.departmentCode  
				            });  
				        });  
				        $('#sweDepartmentName').multiselect('dataprovider',site);
					},function(error) {
						
					});
			    }
	        });  
			
			//部门
			$('#sweDepartmentName').multiselect({
				includeSelectAllOption: true,
				enableFiltering: true,
				delimiterText: '; ',
	            maxHeight: $scope.multiSelectMaxHeight
	        });
		};
		//关闭加载
		$scope.viewEmployeeModel.unLoad=function(){
			$scope.viewEmployeeModel.show=false;
			$scope.viewEmployeeModel.employeeNo="";
		    $scope.viewEmployeeModel.departmentName="";
		    $scope.viewEmployeeModel.roleName="";
		};
		
		//加载数据
		$scope.viewEmployeeModel.bindData = function(){
			$scope.viewEmployeeModel.employeeList=[];
			if($scope.viewEmployeeModel.employeeNo!=null){
				var employeeNoData = $scope.viewEmployeeModel.employeeNo.split("\n");
				for(var i=0;i<employeeNoData.length;i++){
					$scope.viewEmployeeModel.employeeList.push(employeeNoData[i]);
				}
			}
			$scope.viewModel.isLoading=true;
			//获取基本信息
			EmployeeService.getEmployeeList({
				employeeList:$scope.viewEmployeeModel.employeeList,
				roleName:$scope.viewEmployeeModel.roleName,
				departmentList:$scope.viewEmployeeModel.departmentList,
				corporationList:$scope.viewEmployeeModel.corporationList,
				employeeLevelList:$scope.viewEmployeeModel.employeeLevelList,
				//employeeIsSeparated:"0",
				departmentIdStatus:true,
				perPage:$scope.viewEmployeeModel.perPage,
				rowCount:$scope.viewEmployeeModel.rowCount
			},
			function(data){
				$scope.viewEmployeeModel.dataList = data.employeeBeanList;
				$scope.viewEmployeeModel.totalData = data.pageTotal;
				$scope.viewEmployeeModel.addCheckbox();
				$scope.viewEmployeeModel.loadDefaultcheckbox();
			    $scope.viewEmployeeModel.JudgeCheckboxAll();
				$scope.viewModel.isLoading=false;
			},function(error) {
				$rootScope.alertError(error.fault.message);
				$scope.viewModel.isLoading=false;
			});
		};
		
		//选择人员保存
		$scope.viewEmployeeModel.saveEmployee=function(){
			// 选择人员(按钮触发)
			if($scope.viewEmployeeModel.employeeType){
				$scope.editModel.schedulWeek.employeeIdList=[];
				for(var i=0;i<$scope.viewEmployeeModel.checkboxDataList.length;i++){
					if($scope.viewEmployeeModel.checkboxDataList[i].checkbox){
						 $scope.editModel.schedulWeek.employeeIdList.push($scope.viewEmployeeModel.checkboxDataList[i].employeeId);
					 }
				}
			}else{
				// 无规律班次-人员(按钮触发)
				if($scope.editModel.dateStr!=null){
					var date2=new Date($scope.editModel.dateStr);
					for(var ei=0;ei<$scope.editModel.schedulWeek.employeePunchTimeList.length;ei++){
						if($scope.editModel.schedulWeek.employeePunchTimeList[ei].date.getTime()== date2.getTime()){
							// 设置人员集合
							$scope.editModel.schedulWeek.employeePunchTimeList[ei].employeeList=[];
							for(var i=0;i<$scope.viewEmployeeModel.checkboxDataList.length;i++){
								if($scope.viewEmployeeModel.checkboxDataList[i].checkbox){
									$scope.editModel.schedulWeek.employeePunchTimeList[ei].employeeList.push($scope.viewEmployeeModel.checkboxDataList[i]);
								 }
							}
							break;
						}
					}
				}
			}
			$scope.viewEmployeeModel.unLoad();
		};
		
		//默认全员分配
		$scope.viewEmployeeModel.allEmployee=function(){
			//获取基本信息
			EmployeeService.getEmployeeIdList({
				/*employeeList:$scope.viewEmployeeModel.employeeList,
				roleName:$scope.viewEmployeeModel.roleName,
				departmentList:$scope.viewEmployeeModel.departmentList,
				corporationList:$scope.viewEmployeeModel.corporationList,
				employeeLevelList:$scope.viewEmployeeModel.employeeLevelList,*/
				employeeIsSeparated:"0",
				departmentIdStatus:true,
			},
			function(data){
				$scope.editModel.schedulWeek.employeeIdList=data;
				$scope.viewEmployeeModel.checkboxDataList=[];
				for(var i=0;i<$scope.editModel.schedulWeek.employeeIdList.length;i++){
					var cheDate={};
					cheDate.employeeId=$scope.editModel.schedulWeek.employeeIdList[i];
					cheDate.checkbox=true;
					$scope.viewEmployeeModel.checkboxDataList.push(cheDate);
				}
				$scope.viewEmployeeModel.unLoad();
			},function(error) {
				$rootScope.alertError(error.fault.message);
			});
		};
		
		//设置默认复选框值
		$scope.viewEmployeeModel.addCheckbox=function(){
			 for(var i=0;i<$scope.viewEmployeeModel.dataList.length;i++){
				 $scope.viewEmployeeModel.dataList[i].checkbox=false;
			 }
		};
		
		//处理加载复选框选择状态
		$scope.viewEmployeeModel.loadDefaultcheckbox=function(){
			 for(var i=0;i<$scope.viewEmployeeModel.checkboxDataList.length;i++){
				 for(var j=0;j<$scope.viewEmployeeModel.dataList.length;j++){
					 if($scope.viewEmployeeModel.checkboxDataList[i].employeeId==$scope.viewEmployeeModel.dataList[j].employeeId){
						 if($scope.viewEmployeeModel.checkboxDataList[i].checkbox){
							 $scope.viewEmployeeModel.dataList[j].checkbox=true;
						 }else{
							 $scope.viewEmployeeModel.dataList[j].checkbox=false;
						 }
					 }
				 }
			 }
		};
		
		//判断是否为全选
		$scope.viewEmployeeModel.JudgeCheckboxAll=function(){
			 var type=false;
			 for(var i=0;i<$scope.viewEmployeeModel.dataList.length;i++){
				 if($scope.viewEmployeeModel.dataList[i].checkbox==false){
					 type=false;
					 break;
				 }else{
					 type=true;
				 }
			 }
			 if(type){
				 $scope.viewEmployeeModel.checkAll=true;
			 }else{
				 $scope.viewEmployeeModel.checkAll=false;
			 }
		};
		
		//全选事件
		$scope.viewEmployeeModel.checkboxAll=function(){
			//判断是否为全选
			 if($scope.viewEmployeeModel.checkAll==false){
				 angular.forEach($scope.viewEmployeeModel.dataList, function(value, key) {
					 value.checkbox=true;
					 var type=true;
					 for(var i=0;i<$scope.viewEmployeeModel.checkboxDataList.length;i++){
						 if($scope.viewEmployeeModel.checkboxDataList[i].employeeId==value.employeeId){
							 type=false;
							 break;
						 }else{
							 type=true;
						 }
					 }
					 if(type){
						 $scope.viewEmployeeModel.checkboxDataList.push(value);
					 }
				 });
			 }else{
				 angular.forEach($scope.viewEmployeeModel.dataList, function(value, key) {
					 value.checkbox=false;
					 for(var i=0;i<$scope.viewEmployeeModel.checkboxDataList.length;i++){
						 if($scope.viewEmployeeModel.checkboxDataList[i].employeeId==value.employeeId){
							 $scope.viewEmployeeModel.checkboxDataList.splice(i,1);
						 }
					 }
				 });
			 }
		};
		
		//单选事件
		$scope.viewEmployeeModel.checkbox=function(employee){
			 if(employee.checkbox){
				 employee.checkbox=false;
				 for(var i=0;i<$scope.viewEmployeeModel.checkboxDataList.length;i++){
					 if($scope.viewEmployeeModel.checkboxDataList[i].employeeId==employee.employeeId){
						 $scope.viewEmployeeModel.checkboxDataList.splice(i,1);
					 }
				 }
			 }else{
				 employee.checkbox=true;
				 $scope.viewEmployeeModel.checkboxDataList.push(employee);
			 }
			 $scope.viewEmployeeModel.JudgeCheckboxAll();
		};
		
		//控制复选框是否可用
		$scope.viewEmployeeModel.isDisabled=function(){
			if($scope.editModel.schedulWeek.isDefault==1){
				return true;
			}else{
				return false;
			}
		};

		/**************选择考勤打卡时间**************/
		$scope.viewPunchTimeModel = {};
		$scope.viewPunchTimeModel.title="选择考勤时间";
		$scope.viewPunchTimeModel.punchTimeName=null;
		$scope.viewPunchTimeModel.sort=true;
		// 控制开启考勤时间窗口状态
		// true 考勤时间设置（按钮触发） false 时间（按钮触发）
		$scope.viewPunchTimeModel.punchTimeType=true;
		
		//默认员工集合
		$scope.viewPunchTimeModel.radioData=null;
		
		//选中
		$scope.viewPunchTimeModel.radio=function(punchTime){
			$scope.viewPunchTimeModel.radioData=angular.copy(punchTime);
		};
		
		// 打开选择考勤时间
		$scope.viewPunchTimeModel.addPunchTime=function(){
			$scope.viewModel.batchPunchTimeType=false;
			$scope.viewPunchTimeModel.punchTimeType=true;
			$scope.viewPunchTimeModel.load();
		};
		
		//加载
		$scope.viewPunchTimeModel.load=function(){
			$scope.viewPunchTimeModel.punchTimeName=null;
			$scope.viewPunchTimeModel.sort=true;
			$scope.viewPunchTimeModel.bindData();
			$scope.viewPunchTimeModel.show=true;
			//默认员工集合
			$scope.viewPunchTimeModel.radioData=null;
		};
		
		//关闭加载
		$scope.viewPunchTimeModel.unLoad=function(){
			$scope.viewPunchTimeModel.show=false;
		};
		
		//控制升序，降序
        $scope.viewPunchTimeModel.order=function(){
        	if($scope.viewPunchTimeModel.sort){
        		$scope.viewPunchTimeModel.sort=false;
        	}else{
        		$scope.viewPunchTimeModel.sort=true;
        	}
        	$scope.viewPunchTimeModel.bindData();
        };
		
		//加载数据
		$scope.viewPunchTimeModel.bindData = function(){
			$scope.viewModel.isLoading=true;
			//获取基本信息
			PunchTimeService.getPunchTimeList({
				punchTimeName:$scope.viewPunchTimeModel.punchTimeName,
				sort:$scope.viewPunchTimeModel.sort
			},
			function(data){
				$scope.viewPunchTimeModel.dataList = data;
				$scope.viewModel.isLoading=false;
			},function(error) {
				$rootScope.alertError(error.fault.message);
				$scope.viewModel.isLoading=false;
			});
		};
		
		//单选框是否选中
		$scope.viewPunchTimeModel.rdoChecked=function(punchTime){
			// 考勤时间设置(按钮触发)
			if($scope.viewPunchTimeModel.punchTimeType){
				if($scope.editModel.schedulWeek.punchTimeId!=null){
					if(punchTime.punchTimeId==$scope.editModel.schedulWeek.punchTimeId){
						return true;
					}
				}
			}else{
				if($scope.editModel.schedulWeek.employeePunchTimeList!=null){
					// 无规律班次-时间(按钮触发)
					for(var i=0;i<$scope.editModel.schedulWeek.employeePunchTimeList.length;i++){
						if($scope.editModel.dateStr!=null){
							var date2=new Date($scope.editModel.dateStr);
							if($scope.editModel.schedulWeek.employeePunchTimeList[i].date.getTime()== date2.getTime()
									&& $scope.editModel.schedulWeek.employeePunchTimeList[i].punchTimeId==punchTime.punchTimeId){
								return true;
							}
						}
					}
				}
			}
			return false;
		};
		
		//选择人员保存
		$scope.viewPunchTimeModel.save=function(){
			// 批量修改考勤时间
			if($scope.viewModel.batchPunchTimeType){
				if($scope.viewPunchTimeModel.radioData!=null){
					var data={};
					data.swIdList=[];
					data.punchTimeId=$scope.viewPunchTimeModel.radioData.punchTimeId;
					for(var i=0;i<$scope.viewModel.dataList.length;i++){
						if($scope.viewModel.dataList[i].checkbox){
							data.swIdList.push($scope.viewModel.dataList[i].swId);
						}
					}
					$scope.viewModel.isLoading = true;
					Service.batchPunchTime(data,
						function(data){
							$scope.viewPunchTimeModel.unLoad();
							$rootScope.alertInfo("批量修改考勤时间成功。");
							$scope.viewModel.bindData();
							$scope.viewModel.isLoading = false;
						},function(error) {
							$rootScope.alertError(error.fault.message);
							$scope.viewModel.isLoading = false;
						});
				}else{
					$rootScope.alertWarning("请选择考勤时间！");
					return;
				}
			}else{
				// 考勤时间设置(按钮触发)
				if($scope.viewPunchTimeModel.punchTimeType){
					if($scope.viewPunchTimeModel.radioData!=null){
						$scope.editModel.schedulWeek.punchTimeId=$scope.viewPunchTimeModel.radioData.punchTimeId;
						$scope.editModel.schedulWeek.startFromTime=$scope.viewPunchTimeModel.radioData.startFromTime;
						$scope.editModel.schedulWeek.startToTime=$scope.viewPunchTimeModel.radioData.startToTime;
						$scope.editModel.schedulWeek.startInterval=$scope.viewPunchTimeModel.radioData.startInterval;
						$scope.editModel.schedulWeek.endInterval=$scope.viewPunchTimeModel.radioData.endInterval;
						$scope.editModel.schedulWeek.endFromTime=$scope.viewPunchTimeModel.radioData.endFromTime;
						$scope.editModel.schedulWeek.endToTime=$scope.viewPunchTimeModel.radioData.endToTime;
					}
				}else{
					// 无规律班次-时间(按钮触发)
					if($scope.editModel.dateStr!=null){
						var date2=new Date($scope.editModel.dateStr);
						for(var ei=0;ei<$scope.editModel.schedulWeek.employeePunchTimeList.length;ei++){
							if($scope.editModel.schedulWeek.employeePunchTimeList[ei].date.getTime()== date2.getTime()){
								$scope.editModel.schedulWeek.employeePunchTimeList[ei].punchTimeId=$scope.viewPunchTimeModel.radioData.punchTimeId;
								break;
							}
						}
					}
				}
			}
			$scope.viewPunchTimeModel.unLoad();
		};
		
		/**************节假日**************/
		$scope.viewHolidayModel = {};
		$scope.viewHolidayModel.title="查看节假日";
		
		//加载
		$scope.viewHolidayModel.load=function(){
			$scope.viewHolidayModel.show=true;
			$scope.viewHolidayModel.bindData();
		};
		
		//关闭加载
		$scope.viewHolidayModel.unLoad=function(){
			$scope.viewHolidayModel.show=false;
		};
		
		//加载数据
		$scope.viewHolidayModel.bindData = function(){
			$scope.viewModel.isLoading=true;
			//获取基本信息
			HolidayService.getHolidayList({
				holidayYear:(new Date()).getFullYear()
			},
			function(data){
				$scope.viewHolidayModel.dataList = data;
				$scope.viewModel.isLoading=false;
			},function(error) {
				$rootScope.alertError(error.fault.message);
				$scope.viewModel.isLoading=false;
			});
		};
		
		// 获取周几
		$scope.viewHolidayModel.weekDay = function(date){
			var dt=new Date(date);
			var weekDay = ["周天", "周一", "周二", "周三", "周四", "周五", "周六"];
			return weekDay[dt.getDay()];
		};
		
		/*************员工详情*************/
		$scope.viewEmployeeDetailedModel = {};
		$scope.viewEmployeeDetailedModel.title="员工详情";
		$scope.viewEmployeeDetailedModel.schedulWeek={};
		
		// 员工详情
		$scope.viewEmployeeDetailedModel.employeeDetailed = function(schedulWeek){
			$scope.viewEmployeeDetailedModel.schedulWeek=schedulWeek;
			$scope.viewEmployeeDetailedModel.load();
		};
		
		//加载
		$scope.viewEmployeeDetailedModel.load=function(){
			$scope.viewEmployeeDetailedModel.show=true;
			$scope.viewEmployeeDetailedModel.bindData();
		};
		
		//关闭加载
		$scope.viewEmployeeDetailedModel.unLoad=function(){
			$scope.viewEmployeeDetailedModel.show=false;
		};
		
		//加载数据
		$scope.viewEmployeeDetailedModel.bindData = function(){
			$scope.viewModel.isLoading=true;
			//获取基本信息
			Service.getSchedulWeekEmployeeDetailedList({
				swId:$scope.viewEmployeeDetailedModel.schedulWeek.swId
			},
			function(data){
				$scope.viewEmployeeDetailedModel.dataList = data;
				$scope.viewModel.isLoading=false;
			},function(error) {
				$rootScope.alertError(error.fault.message);
				$scope.viewModel.isLoading=false;
			});
		};
		
		/*********************************/
		
		/**************串休**************/
		$scope.viewSwRestModel = {};
		$scope.viewSwRestModel.title="选择串休";
		$scope.viewSwRestModel.employeeId=null;
		$scope.viewSwRestModel.employeeIdRest=null;
		$scope.viewSwRestModel.dateRest=null;
		$scope.viewSwRestModel.swId=null;
		$scope.viewSwRestModel.date=null;
		$scope.viewSwRestModel.currentDataList=[];
		
		//加载
		$scope.viewSwRestModel.load=function(){
			$scope.viewSwRestModel.show=true;
			$scope.viewSwRestModel.currentBindData();
			$scope.viewSwRestModel.allBindData();
		};
		
		//关闭加载
		$scope.viewSwRestModel.unLoad=function(){
			$scope.viewSwRestModel.show=false;
			$scope.viewSwRestModel.employeeId=null;
			$scope.viewSwRestModel.employeeIdRest=null;
			$scope.viewSwRestModel.dateRest=null;
		};
		
		//选中
		$scope.viewSwRestModel.radio=function(employee,swName){
			// 当前日期
			if(swName=="current"){
				$scope.viewSwRestModel.employeeId=employee.employeeId;
			}
			// 全部日期
			else if(swName=="all"){
				$scope.viewSwRestModel.employeeIdRest=employee.employeeId;
				$scope.viewSwRestModel.dateRest=employee.fromDate;
			}
		};
		
		//单选框是否选中
		$scope.viewSwRestModel.rdoChecked=function(employee,swName){
			// 当前日期
			if(swName=="current"){
				if($scope.viewSwRestModel.employeeId!=null){
					if(employee.employeeId==$scope.viewSwRestModel.employeeId){
						return true;
					}
				}
			}
			// 全部日期
			else if(swName=="all"){
				if($scope.viewSwRestModel.employeeIdRest!=null){
					if(employee.employeeId==$scope.viewSwRestModel.employeeIdRest
							&& employee.fromDate==$scope.viewSwRestModel.dateRest){
						return true;
					}
				}
			}
			return false;
		};
		
		// 全部数据单选框是否可用
		$scope.viewSwRestModel.rdoDisabled=function(employee){
			if($scope.viewSwRestModel.employeeId==null){
				return true;
			}else{
				// 判断员工ID相等，单选框不可用
				for(var i=0;i<$scope.viewSwRestModel.currentDataList.length;i++){
					if($scope.viewSwRestModel.currentDataList[i].employeeId==employee.employeeId){
						return true;
					}
				}
			}
			return false;
		};
		
		//串休保存
		$scope.viewSwRestModel.save=function(){
			// 无规律班次-时间(按钮触发)
			if($scope.editModel.dateStr!=null){
				var date2=new Date($scope.editModel.dateStr);
				for(var ei=0;ei<$scope.editModel.schedulWeek.employeePunchTimeList.length;ei++){
					if($scope.editModel.schedulWeek.employeePunchTimeList[ei].date.getTime()== date2.getTime()){
						$scope.editModel.schedulWeek.employeePunchTimeList[ei].employeeId=$scope.viewSwRestModel.employeeId;
						$scope.editModel.schedulWeek.employeePunchTimeList[ei].employeeIdRest=$scope.viewSwRestModel.employeeIdRest;
						$scope.editModel.schedulWeek.employeePunchTimeList[ei].dateRest=$scope.viewSwRestModel.dateRest;
						break;
					}
				}
			}
			$scope.viewSwRestModel.unLoad();
		};
		
		//加载当前日期下员工数据
		$scope.viewSwRestModel.currentBindData = function(){
			$scope.viewModel.isLoading=true;
			//获取基本信息
			Service.getSchedulWeekEmployeeListBySwId({
				swId:$scope.viewSwRestModel.swId,
				fromDate:$scope.viewSwRestModel.date,
				thruDate:$scope.viewSwRestModel.date
			},
			function(data){
				$scope.viewSwRestModel.currentDataList = data;
				$scope.viewModel.isLoading=false;
			},function(error) {
				$rootScope.alertError(error.fault.message);
				$scope.viewModel.isLoading=false;
			});
		};
		
		//加载日期下全部员工数据
		$scope.viewSwRestModel.allBindData = function(){
			$scope.viewModel.isLoading=true;
			//获取基本信息
			Service.getSchedulWeekEmployeeListBySwId({
				swId:$scope.viewSwRestModel.swId,
				date:$scope.viewSwRestModel.date
			},
			function(data){
				$scope.viewSwRestModel.allDataList = data;
				$scope.viewModel.isLoading=false;
			},function(error) {
				$rootScope.alertError(error.fault.message);
				$scope.viewModel.isLoading=false;
			});
		};
		
		/*********************************/
		
		/**************导入**************/
		$scope.importModel = {};
		$scope.importModel.title="无规律班次导入";
        $scope.importModel.uploadStream = {};
        $scope.importModel.attachmentId = null;
        $scope.importModel.messageHideType=false;
        $scope.importModel.successHideType=false;
        $scope.importModel.message=null;
        $scope.importModel.isArray=false;
        
		//加载
		$scope.importModel.load=function(){
	        $scope.importModel.messageHideType=false;
	        $scope.importModel.successHideType=false;
	        $scope.importModel.message=null;
	        $scope.importModel.isArray=false;
			$scope.importModel.show=true;
		};
		
		//关闭加载
		$scope.importModel.unLoad=function(){
			$scope.importModel.show=false;
		};
		
		// 模板下载
		$scope.importModel.getTemplate = function () {
            $rootScope.isLoading = true;
            Service.getTemplateUrl(function (data) {
                $rootScope.isLoading = false;
                window.location.href = data;
            }, function () {
                $rootScope.isLoading = false;
                $scope.importModel.messageHideType=true;
                $scope.importModel.message="系统错误";
                $scope.importModel.isArray=false;
            });
        };
        
        // 上传
        $scope.importModel.fileUploadComplete    = function (data) {
            $scope.importModel.messageHideType=false;
            $scope.importModel.successHideType=false;
            var dataList=data;
            if (dataList[0]['attachment'] != null) {
                $scope.$apply(function () {
                    $scope.importModel.attachmentId = dataList[0]['attachment']['attachmentId'];
                    $scope.importModel.save();
                });
            }else{
            	$scope.importModel.message="请先上传文件!";
                $scope.importModel.isArray=false;
            }
        };
		
        // 保存
        $scope.importModel.save = function () {
            $rootScope.isLoading = true;
            Service.schedulWeekImport({
                attachmentId: $scope.importModel.attachmentId
            }, function (data) {
                $rootScope.isLoading = false;
                if (data['result'] == 1) {
                    $scope.importModel.successHideType=true;
                    if(data['success'].length<=0){
                    	$scope.importModel.attachmentId=null;
                        $scope.importModel.isArray = false;
                        $scope.importModel.message = "导入完成。";
                    }else{
                        $scope.importModel.isArray = true;
                        $scope.importModel.message = data['success'];
                    }
                    $scope.viewModel.rowCount=1;
                    $scope.viewModel.bindData();
                } else {
                    $scope.importModel.messageHideType=true;
                    $scope.importModel.message="系统错误";
                    $scope.importModel.isArray=false;
                }
            }, function (data) {
                $rootScope.isLoading      = false;
                $scope.importModel.attachmentId = null;
                $scope.importModel.uploadStream.cancel();
                $scope.importModel.messageHideType=true;
                if (angular.isArray(data['fault'])) {
                    var faultMessage = [data['fault'][0]['message']].concat(data['fault'][0]['errorData']);
                    $scope.importModel.message=faultMessage;
                    $scope.importModel.isArray=true;
                    //$rootScope.alertError(faultMessage);
                } else {
                    $scope.importModel.message=data['fault']['message'];
                    $scope.importModel.isArray=false;
                    //$rootScope.alertError(data['fault']['message']);
                }
            });
        };
		/*********************************/
} ]);